package com.bwie.redkids;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;

public class MainActivity extends Activity {

	private ViewPager vp_navigation;
	private SharedPreferences sha;
	private ArrayList<Integer> arrayList;
	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			if (msg.what == 0) {
				int time = (Integer) msg.obj;
				System.out.println("------------------------time------------"
						+ time);
				if (time == 2) {
					Intent intent = new Intent(MainActivity.this,
							IndexActivity.class);
					startActivity(intent);
					MainActivity.this.finish();
				}
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		vp_navigation = (ViewPager) findViewById(R.id.vp_navigation);
		sha = getSharedPreferences("info.xml", MODE_PRIVATE);
		if (sha.getBoolean("flag", false)) {
			MainActivity.this.finish();
			Intent intent = new Intent(MainActivity.this, IndexActivity.class);
			startActivity(intent);
		} else {
			// 初始化数据
			initView();
			// 申请数据
			initData();
		}
	}

	/**
	 * 申请数据
	 * */
	private void initData() {
		// 适配器
		vp_navigation.setAdapter(new PagerAdapter() {

			@Override
			public boolean isViewFromObject(View arg0, Object arg1) {
				// TODO Auto-generated method stub
				return arg0 == arg1;
			}

			@Override
			public int getCount() {
				// TODO Auto-generated method stub
				return arrayList.size();
			}

			@Override
			public void destroyItem(ViewGroup container, int position,
					Object object) {
				// super.destroyItem(container, position, object);
				container.removeView((View) object);
			}

			@Override
			public Object instantiateItem(ViewGroup container, int position) {
				View view = View.inflate(MainActivity.this,
						R.layout.navigation_item, null);
				ImageView iv_navigation = (ImageView) view
						.findViewById(R.id.iv_navigation);
				iv_navigation.setImageResource(arrayList.get(position));
				container.addView(view);
				return view;
			}
		});
		// 设置viewPager监听
		vp_navigation.setOnPageChangeListener(new OnPageChangeListener() {

			private int time = 3;;
			private Timer timer;
			private TimerTask task;

			@Override
			public void onPageSelected(int posotion) {
				if (posotion == 4) {
					// 保存数据
					Editor edit = sha.edit();
					edit.putBoolean("flag", true);
					edit.commit();

					timer = new Timer();
					task = new TimerTask() {
						private Message msg;

						@Override
						public void run() {
							time--;
							msg = Message.obtain();
							msg.obj = time;
							msg.what = 0;
							handler.sendMessage(msg);
						}
					};
					timer.schedule(task, 3000);
				}
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub

			}
		});
	}

	/**
	 * 初始化数据
	 * */
	private void initView() {

		arrayList = new ArrayList<Integer>();
		arrayList.add(R.drawable.navigation1);
		arrayList.add(R.drawable.navigation2);
		arrayList.add(R.drawable.navigation3);
		arrayList.add(R.drawable.navigation4);
		arrayList.add(R.drawable.navigation5);
	}

}
